<template>
  <el-button type="text" size="small" :disabled="topDisabled" @click="handleMediaGoodsToTop(1)">置顶</el-button>
  <el-button type="text" size="small" :disabled="cancelTopDisabled" @click="handleMediaGoodsToTop(0)">取消置顶</el-button>
</template>

<script>
  import {mediaGoodsToTop} from "@/api/goods/relgoods";
  import {ElMessageBox, ElMessage} from 'element-plus'

  export default {
    name: "MediaGoodsToTop",
    props: {
      row: {
        type: Object,
        required: true
      },
      topDisabled: {
        type: Boolean,
        default: false
      },
      cancelTopDisabled: {
        type: Boolean,
        default: false
      },
      mediaid: {
        type: Number,
        default: undefined
      }
    },
    emits: ['reloadGoodsList'],
    setup(props, context){
      const reloadGoods = () => {
        context.emit('reloadGoodsList')
      }
      const handleMediaGoodsToTop = (iftop) => {
        const data = {mediaid: props.mediaid, goodsid: props.row.goods_id, iftop: iftop}
        const title = iftop == 0 ? '确定要将商品' + props.row.goods_name + '取消置顶吗？' : '确定要将商品' + props.row.goods_name + '置顶吗？'
        ElMessageBox.confirm(title, '', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          mediaGoodsToTop(data).then(response => {
            if (response.errcode === 0) {
              ElMessage({
                type: 'success',
                message: '操作成功',
                duration: 2000,
                onClose: () => {
                  reloadGoods()
                }
              })
            }
          })
        }).catch(() => {})
      }

      return {
        handleMediaGoodsToTop
      }
    }
  }
</script>

<style scoped>

</style>
